home *** CD-ROM | disk | FTP | other *** search
/ Power Tools for Macintosh / Power Tools for Macintosh (SoftBit)(1992).iso / Applications / Alpha 4.01 / ACMDS / SndPlay.c < prev    next >
C/C++ Source or Header  |  1990-08-15  |  4KB  |  139 lines

  1. /****************************************************************
  2. *                                                               *
  3. * SndPlay - play the named 'snd ' resource                      *
  4. *                                                               *
  5. * Copyright © 1990 Karl J. Smith. All Rights Reserved           *
  6. *                                                               *
  7. * email: ksmith@jarthur.claremont.edu                           *
  8. *                                                               *
  9. * 8/13/90                                                       *
  10. *                                                               *
  11. * Changes:                                                      *
  12. * --------                                                      *
  13. * 8/15/90 KJS        Now plays if it hits either end of          *
  14. *                     stream or a carriage return.                *
  15. *                                                               *
  16. * When debugging, set the project type to application,          *
  17. * and add the ANSI project.                                     *
  18. *                                                               *
  19. * When all your bugs are out, 'undef' DEBUG, set the project    *
  20. * type to Code Resource, with type = ACMD, name = <the name     *
  21. * of the function>, file type to 'AEXT', and the purgeable      *
  22. * flag set to true.                                             *
  23. *                                                               *
  24. * Also, you need to remove the ANSI project from the ACMD       *
  25. * project and replace it with the ANSI-A4 project if you use    *
  26. * any functions in ANSI, such as strlen.                        *
  27. *                                                               *
  28. * If you are not using THINK C, I'm not quite sure what you     *
  29. * should do. :-)                                                *
  30. *                                                               *
  31. ****************************************************************/
  32.  
  33. /*
  34. **    This ACMD will attempt load and play (sequentially) all 'snd '
  35. **  resources that are listed in the input string, separated by
  36. **  '\r' characters. When a '\r' character is reached, the previous
  37. **  sound will be played (if possible).
  38. */
  39.  
  40. #undef    DEBUG
  41.  
  42. #include <stdio.h>
  43. #include <string.h>
  44.  
  45. /* Prototypes */
  46. char *
  47. #ifdef    DEBUG
  48. dummy(char *inStr);
  49. #else    DEBUG
  50. main(char *inStr);
  51. #endif    DEBUG
  52.  
  53. char *
  54. #ifdef    DEBUG
  55. dummy(inStr)
  56. #else    DEBUG
  57. main(inStr)
  58. #endif    DEBUG
  59. char    *inStr;
  60. {
  61.     char *currentChar;
  62.     Handle sndHandle;
  63.     
  64.     Str255 soundName;    /* Name of current sound to play */
  65.     char *currentCharOfName;    /* used to create name of sound to play */
  66.     
  67.     currentCharOfName = (char *)soundName;
  68.     
  69.     for (currentChar = inStr; *currentChar != 0; currentChar++)
  70.         {
  71.             if ( (*currentChar == '\r') || (*(currentChar + 1L) == '\0'))
  72.                 {
  73.                     if (  (*(currentChar + 1L) == '\0') /* No return at end of line */
  74.                         && (*currentChar != '\r') )
  75.                     {
  76.                         *currentCharOfName++ = *currentChar;
  77.                     }
  78.                     
  79.                     /* Try to play the sound */
  80.                     *currentCharOfName = '\0';    /* End of name */
  81.                     CtoPstr((char *)soundName);
  82.                     sndHandle = GetNamedResource('snd ', soundName);
  83.                     if (sndHandle != NULL)
  84.                         {
  85.                             SndPlay(NULL, sndHandle, FALSE);
  86.                             ReleaseResource(sndHandle);
  87.                             DisposHandle(sndHandle);
  88.                         }
  89.                     currentCharOfName = (char *)soundName;    /* Start at beginning of name */
  90.                 }
  91.             else 
  92.                 {
  93.                     *currentCharOfName++ = *currentChar;
  94.                     
  95.                 }
  96.         }
  97.     return(inStr);
  98. }
  99.  
  100.  
  101. #ifdef    DEBUG
  102.  
  103. char * ConvertRtoN(char *theStr);
  104.  
  105. char * ConvertRtoN(theStr)
  106.     char *theStr;
  107. {
  108.     char *currentChar;
  109.     
  110.     for (currentChar = theStr;*currentChar != 0; currentChar++)
  111.         if (*currentChar == '\r')
  112.             *currentChar = '\n';
  113. }
  114.     
  115. main()
  116. {
  117.     char    *str = "Boing\rMonkey\r";
  118.     char    *ret;
  119.     char    once[255];
  120.         
  121.     strcpy(once, str);                
  122.     
  123.     ret = dummy(once);
  124.     
  125.     ConvertRtoN(ret);                /* Convert '\r' to '\n' so we can see them in a printf */
  126.     ConvertRtoN(str);
  127.         
  128.     printf("The original is:\n\n%s", str);
  129.     printf("\n----\n\nThe result is:\n\n%s", ret);
  130.     
  131. }
  132. #endif    DEBUG
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.